import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TcpClient {
    public static void main(String[] args) throws Exception{
        try{
            //创建socket
            Socket socket=new Socket("127.0.0.1",4700);
            //建立连接
            InputStreamReader systemIn=new InputStreamReader(System.in);
            BufferedReader systemReader = new BufferedReader(systemIn);

            InputStreamReader socketIn=new InputStreamReader(socket.getInputStream());
            BufferedReader socketReader = new BufferedReader(socketIn);

            PrintWriter socketWriter = new PrintWriter(socket.getOutputStream());

            //进行通信
            String readLine = systemReader.readLine();
            while (!readLine.equals("over")){
                socketWriter.println(readLine);
                socketWriter.flush();//刷一下缓冲区
                System.out.println("客户端"+readLine);
                System.out.println("服务端"+socketReader.readLine());//等待服务器发送，不发就一直停在这
                readLine=systemReader.readLine();//从键盘获取值
            }
            //关闭
            socketWriter.close();
            socketIn.close();
            socket.close();
        }catch (Exception e){
            System.out.println("Error:"+e);
        }
    }
}
